package org.jeecg.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.system.entity.SysDocking;
import org.jeecg.modules.system.service.ISysDockingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: 三方管理
 * @Author: jeecg-boot
 * @Date: 2023-03-08
 * @Version: V1.0
 */
@Api(tags = "三方管理")
@RestController
@RequestMapping("/sys/docking")
@Slf4j
public class SysDockingController extends JeecgController<SysDocking, ISysDockingService> {

    @Autowired
    private ISysDockingService sysDockingService;


    @ApiOperation(value = "三方管理-分页列表查询", notes = "三方管理-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<SysDocking>> queryPageList(SysDocking sysDocking,
                                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                   HttpServletRequest req) {
        QueryWrapper<SysDocking> queryWrapper = QueryGenerator.initQueryWrapper(sysDocking, req.getParameterMap());
        Page<SysDocking> page = new Page<>(pageNo, pageSize);
        IPage<SysDocking> pageList = sysDockingService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @AutoLog(value = "三方管理-添加")
    @ApiOperation(value = "三方管理-添加", notes = "三方管理-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody SysDocking sysDocking) {
        String key = oConvertUtils.randomGen(16);
        String token = CommonConstant.PREFIX_SANFANG_TOKEN + JwtUtil.sign(sysDocking.getCode(), key);
        sysDocking.setKeyText(key);
        sysDocking.setToken(token);
        sysDockingService.save(sysDocking);
        return Result.OK("添加成功！");
    }

    @AutoLog(value = "三方管理-编辑")
    @ApiOperation(value = "三方管理-编辑", notes = "三方管理-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody SysDocking sysDocking) {
        sysDockingService.updateById(sysDocking);
        return Result.OK("编辑成功!");
    }

    @AutoLog(value = "三方管理-通过id删除")
    @ApiOperation(value = "三方管理-通过id删除", notes = "三方管理-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id") String id) {
        sysDockingService.removeById(id);
        return Result.OK("删除成功!");
    }

    @ApiOperation(value = "三方管理-通过id查询", notes = "三方管理-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<SysDocking> queryById(@RequestParam(name = "id") String id) {
        SysDocking sysDocking = sysDockingService.getById(id);
        if (sysDocking == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(sysDocking);
    }

}
